<html>
<head>
	<!--	Written by Everett Carney Wilson (NegativeK on the intertubes.)
	  	Licensed under new BSD license. See EOF. --!>
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
	<script type="text/javascript">
		var characters = new Array();

		function addCharacter() {
			var name, speed, init, mods;
			var character;
				
			name = $('div.addCharForm input:text[name=charname]').val();
			mods = $('div.addCharForm input:text[name=mods]').val();
			speed = $('div.addCharForm input:text[name=speed]').val();
			init = $('div.addCharForm input:text[name=init]').val();

			if(mods == "") {
				mods = 0;
			}

			mods = parseInt(mods);
			speed = parseInt(speed);
			init = parseInt(init);

			if(validateSaveForm(name, speed, init, mods)) {
				character = new Character(name, speed, init, mods);
				characters.push(character);
				redrawTable(false);
				resetForm();
			}
		}

		function modifyChar() {
			var editChar, charIndex;

			charIndex = selectedChar();
			editChar = characters[charIndex];

			if(typeof(editChar) != "undefined") {
				$('.addCharForm > input[name=charname]').val(editChar.name);
				$('.addCharForm > input[name=speed]').val(editChar.speed);
				$('.addCharForm > input[name=init]').val(editChar.init);
				$('.addCharForm > input[name=mods]').val(editChar.mods);

				removeChar(charIndex);
			}
		}

		function removeChar(charIndex) {
			if(charIndex == -1) {
				charIndex = selectedChar();
			}

			characters.splice(charIndex, 1);
			redrawTable(false);
		}

		function selectedChar() {
			var name, charIndex;

			charIndex = -1;
			name = $('tr.currentTurn > td:nth-child(1)').text();

			for(var index = 0; index < characters.length; index++) {
				if(characters[index].name == name) {
					charIndex = index; 
					break;
				}
			}
			
			return charIndex;
		}

		function redrawTable(newRound) {
			var initRolls = new Array();
			var roundChars = characters;

			if(newRound) {
				for(var index = 0; index < characters.length; index++) {
					characters[index].rollInit();
				}
			}

			characters.sort(compareRolledInits);

			$('table.chars').empty();
			$('table.chars').append(
				"\t\t<tr>\n" + 
				"\t\t\t<td> Name </td>\n" + 
				"\t\t\t<td> Speed </td>\n" + 
				"\t\t\t<td> Init </td>\n" + 
				"\t\t\t<td> Mods </td>\n" + 
				"\t\t\t<td> Roll </td>\n" + 
				"\t\t\t<td> Result </td>\n" + 
				"\t\t</tr>\n" 
			);

			for(var speed = 1; speed <= 4; speed++) {
				for(var index = 0; index < characters.length; index++) {
					if(characters[index].speed >= speed) {
						var tableRow;

						tableRow = characters[index].getTableRow();

						$('table.chars').append(tableRow);
					}
				}
			}

			$('table.chars > tr:nth-child(2)').addClass('currentTurn');
		}

		function highlightNextTurn() {
			var currentTurn, nextTurn;
			
			currentTurn = $('table.chars > tr.currentTurn');
			nextTurn = $('table.chars > tr.currentTurn').next();
			currentTurn.toggleClass('currentTurn');

			if(nextTurn.length != 0) {
				nextTurn.toggleClass('currentTurn');
			}
			else {
				redrawTable(true);
			}
		}

		function validateSaveForm(name, speed, init, mods) {
			var returnValue, whitelistPattern;

			returnValue = true;

			/* Whitelist user input. */
			whitelistPattern = /[^a-z 0-9.\-_,]/i;

			if(name == "" || whitelistPattern.test(name)) {
				$('input[name=charname]').addClass("formError");
				returnValue = false;
			}

			if(isNaN(speed) || speed < 1 || 4 < speed) {
				$('input[name=speed]').addClass("formError");
				returnValue = false;
			}

			if(isNaN(init)) {
				$('input[name=init]').addClass("formError");
				returnValue = false;
			}

			if(isNaN(mods)) {
				$('input[name=mods]').addClass("formError");
				returnValue = false;
			}

			for(var index = 0; index < characters.length; index++) {
				if(characters[index].name == name) {
					$('input[name=charname]').addClass("formError");
					returnValue = false;

					break;
				}
			}

			return returnValue;
		}

		function resetForm() {
			$('div.addCharForm > input').removeClass('formError');
			$('div.addCharForm > input[type=text]').val('');
		}
	</script>

	<script type="text/javascript">
		/* Character class */

		function Character(name, speed, init, mods) {
			this.name = name;
			this.speed = speed;
			this.init = init;
			this.mods = mods;
			this.initRoll = Math.floor(Math.random()*100);
			this.roundInit = this.init + this.mods + this.initRoll;
		}

		function compareRolledInits(char1, char2) {
			return char2.roundInit - char1.roundInit;
		}

		Character.prototype.rollInit = function() {
			this.initRoll = Math.floor(Math.random()*100);
			this.roundInit = this.init + this.mods + this.initRoll;
		}

		Character.prototype.getTableRow = function() {
			var rowString, charName, charInit;

			charName = this.name;
			charSpeed = this.speed;
			charInit = this.init;
			charMods = this.mods;
			charRoll = this.initRoll;
			charRslt = this.roundInit;

			rowString = "\t\t<tr>\n";
			rowString += "\t\t\t<td>" + charName + "</td>\n";
			rowString += "\t\t\t<td>" + charSpeed + "</td>\n";
			rowString += "\t\t\t<td>" + charInit + "</td>\n";
			rowString += "\t\t\t<td>" + charMods + "</td>\n";
			rowString += "\t\t\t<td>" + charRoll + "</td>\n";
			rowString += "\t\t\t<td>" + charRslt + "</td>\n";
			rowString += "\t\t</tr>\n";

			return rowString;
		}
	</script>

	<style type="text/css">
		table, td {
			border-color: #000;
			border-style: solid;
		}

		table {
			border-width: 0 0 1px 1px;
			border-spacing: 0;
			border-collapse: collapse;
		}

		td {
			margin: 0;
			padding: 2px;
			border-width: 1px 1px 0 0;
		}
		
		.currentTurn {
			background-color: #888888;
		}

		.formError {
			background-color: #FF8888;
		}

		.formWhitelistError {
			display: inline;
			font-size: 10px;
			color: #999999;
		}
	</style>
</head>
<body>
	<table class="chars" onClick="highlightNextTurn()">
	</table>
	<br />
	<div class="addCharForm">
		<div class="formWhitelistError">Please stick to alphanumerics, spaces, commas, hyphens, periods, and underscores. </div><br />
		Character Name:	<input type="text" name="charname" /><br />
		Speed:		<input type="text" name="speed" /><br />
		Base Init:	<input type="text" name="init" /><br />
		Modifiers:	<input type="text" name="mods" /><br />
		<button type="button" onClick="addCharacter()">Save</button>
		<button type="button" onClick="modifyChar()">Edit</button>
		<button type="button" onClick="removeChar(-1)">Delete</button>
		<button type="button" onClick="redrawTable(true)">Roll</button>
	</div>
</body>
<!-- License time.

Copyright (c) 2010, Everett Carney Wilson
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of the eptools nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

End license time. -->
</html>
